﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Shared
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Learning;
    using SharingCenter.Web.Classes.enums;
    using System.IO;
    public partial class LearningProfileItem : System.Web.UI.UserControl
    {
        private Guid _lpid;
        private Sc_LearningProfile _profileItem = null;
        private List<Sc_LearningProfile> _list = null;
        public Guid LearningProfileId
        {
            get { return _lpid; }
            set
            {
                _lpid = value;
                _list = new List<Sc_LearningProfile>();
                _list.Add(LearningOperations.GetProfileByProfileId(value));
                
            }
        }

        public Sc_LearningProfile LearningProfileDataItem {
            get { return _profileItem; } 
            set 
            {
                _list = new List<Sc_LearningProfile>();
                _profileItem = value;
                if (value != null)
                {
                    _list.Add(value);
                }
            }
        }
        public bool ShowButtons { get; set; }
        public event EventHandler<EventArgs> ItemDeleted;
        public string CssClass
        {
            get { return FormView1.CssClass; }
            set
            {
                if (value != null)
                {
                    FormView1.CssClass += " " + value;
                }
            }
            
        }
        public bool HighlightSkills { get; set; }

        protected void Page_Init(object sender, EventArgs e)
        {
            //this.Init += new EventHandler(LearningProfileItem_Init);
            
        }

        
        protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
            if (e.CommandName == "deleteitem")
            {
                LearningOperations.DeleteProfile(new Guid(e.CommandArgument.ToString()));
                ItemDeleted(this, new EventArgs());
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            FormView1.DataBound += new EventHandler(FormView1_DataBound);
            FormView1.ItemDeleting += new FormViewDeleteEventHandler(FormView1_ItemDeleting);
            FormView1.ItemCommand += new FormViewCommandEventHandler(FormView1_ItemCommand);
            FormView1.DataSource = _list;
            FormView1.DataBind();
        }

        ////protected override void Render(HtmlTextWriter writer)
        ////{
        ////    CreateChildControls();

        ////    FormView1.DataSource = _list;
        ////    FormView1.DataBind();
        
        ////    //base.Render(writer);
        ////}


        protected void Label_PreRender(object sender, EventArgs e)
        {
            ((Label)sender).CssClass = HighlightSkills ? "skill" : "";
        }

        void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
        {
            
        }

        void FormView1_DataBound(object sender, EventArgs e)
        {
            Sc_LearningProfile pro = (Sc_LearningProfile)FormView1.DataItem ?? _profileItem;
            //Label daysInWeek = (Label)FormView1.FindControl("lblDaysInWeek");
            //daysInWeek.Text = ConvertDaysInWeek(pro.DaysInWeek);
            //daysInWeek.PreRender += new EventHandler(DaysInWeek_PreRender);
            try
            {
                ListView lvInput = (ListView)FormView1.FindControl("lvIoInput");
                ListView lvOutput = (ListView)FormView1.FindControl("lvIoOutput");
                lvInput.DataSource = from t in ((Sc_LearningProfile)FormView1.DataItem).Sc_LearningIOs
                                     where t.Direction == LearningSkillDirection.Input
                                     select t;
                lvOutput.DataSource = from t in ((Sc_LearningProfile)FormView1.DataItem).Sc_LearningIOs
                                      where t.Direction == LearningSkillDirection.Output
                                      select t;
                Panel pnButton = (Panel)FormView1.FindControl("pnButton");
                pnButton.Visible = ShowButtons;
                lvInput.DataBind();
                lvOutput.DataBind();
            }
            catch { }
        }

        void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            ItemDeleted(this, new EventArgs());
        }

        protected void Buttons_PreRender(object sender, EventArgs e)
        {
            ((Panel)sender).Visible = ShowButtons;
        }

        string ConvertDaysInWeek(string dbstr)
        {
            char[] s = { '|' };
            string[] a = dbstr.Split(s);
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = WebConsts.DAYS_OF_WEEK[a[i]];
            }
            return string.Join(", ", a);
        }

        protected void DaysInWeek_PreRender(object sender, EventArgs e)
        {
            try
            {
                Label t = (Label)sender;
                t.Text = ConvertDaysInWeek(t.Text);
            }
            catch { }
        }
    }
}